<?php

class tournaments {
	
	public function __construct(){
		FunctionsHelper::includeFiles(
			array(
				PUBLIC_MODELS_DIRECTORY . 'model.php',
				PUBLIC_MODELS_DIRECTORY . 'tournament.php',
				PUBLIC_MODELS_DIRECTORY . 'tournament_class.php',
				PUBLIC_MODELS_DIRECTORY . 'round.php',
				PUBLIC_MODELS_DIRECTORY . 'match.php',
				PUBLIC_MODELS_DIRECTORY . 'player.php',
				PUBLIC_MODELS_DIRECTORY . 'country.php',
				PUBLIC_MODELS_DIRECTORY . 'serie.php',
				PUBLIC_CONTROLLERS_DIRECTORY . 'tournament_classes.php'
			)
		);
	}

	public function getNations($internationalTournament, $class_id){
		$Tournament = explode("-", $internationalTournament);
		$Name = str_replace("_", " ", $Tournament[0]);
		
		$nationTournaments = $this->getAllTournamentFromClass($class_id);
		$tournament = new Tournament(
			array (
				"name" => $internationalTournament,
				"class" => $class_id,
				"sex" => $Tournament[1]
			)
		);
		$tournament = $tournament->getOneByName();
	}

	public function getOne($description){
		$TournamentClasses = new tournament_classes();
		$tournament = new Tournament(
			array(
				'id' => (is_array($description) && array_key_exists('id', $description)) ?  $description['id'] :  $description
			)
		);

		$TournamentChosen = $tournament->getOne();
		if (empty($TournamentChosen))
			return new tournament();
		$class_id = $TournamentChosen[0]['class_id'];
		$TournamentChosen = new tournament($TournamentChosen[0]);
		$TournamentClass = $TournamentClasses->getOne($class_id);
		$TournamentChosen->setClass($TournamentClass);

		return $TournamentChosen;
	}

	public function getAllTournamentFromClass($ClassId){
		$TournamentsFromClass = array();
		$TournamentClasses = new tournament_classes();
		$TournamentClass = $TournamentClasses->getObjectWith(
			array(
				'id' => $ClassId
			)
		);
		$Tournament = new tournament(
			array(
				'class' => $TournamentClass,
				'order_by' => 'initial_date'
			)
		);

		$arrTournaments = $Tournament->getTournamentsFromClass();
		
		foreach ($arrTournaments as $tournamentSelected){
			$TournamentsFromClass[FunctionsHelper::ReplaceAccentsAndEnie(str_replace(' ', '_', strtolower($tournamentSelected['name'] . "-" . $tournamentSelected['sex'])))] = new tournament(
				array (
					'id' => $tournamentSelected['tournament_id'],
					'class' => $TournamentClass,
					'surface' => $tournamentSelected['surface'],
					'name' => trim($tournamentSelected['name']),
					'place' => $tournamentSelected['place'],
					'description' => $tournamentSelected['description'],
					'initial_date' => $tournamentSelected['initial_date'],
					'final_date' => $tournamentSelected['final_date'],
					'image' => $tournamentSelected['image'],
					'link' => $tournamentSelected['link'],
					'sex' => $tournamentSelected['sex'],
					'points' => $tournamentSelected['points'],
					'active' => $tournamentSelected['active']
				)
			);
		}
		return $TournamentsFromClass;
	}
	
	public function getAllMatches($tournament_id, $isInternational = 0){
		$Round = new Round();
		$arrRounds = $Round->getAll();
		return $this->_getRounds($arrRounds, $tournament_id, $isInternational);
	}
	
	private function _getRounds($arrRounds, $tournament_id, $isInternational = 0){
		$rounds = array();
		foreach ($arrRounds as $round){
			$selectedRound = new Round($round);
			if (!$isInternational){
				$matches = $selectedRound->getAllMatches($tournament_id);
				foreach ($matches as &$match) 	if($match['result'] == 'Resultado') $match['result'] = 'vs.';
				$selectedRound->setMatches($this->_getMatch($matches));
			}else{
				$series = $selectedRound->getAllSeries($tournament_id);

				foreach ($series as &$serie){
					$arrMatches = $this->_getInternationalMatches($serie['serie_id']);
					$serie['country1'] = $serie['country1_id'];
					$serie['country2'] = $serie['country2_id'];
					$serie['matches'] = $arrMatches;
					if($serie['result'] == 'Resultado' || empty($serie['result'])) $serie['result'] = 'vs.';
					$serie = new serie($serie);
				}
				$selectedRound->setMatches($series);
			}
			$rounds[] = $selectedRound;
		}

		return $rounds;
	}

	private function _getInternationalMatches($serie_id){
		$arrMatches = array();

 		$match = new Match();
 		$matches = $match->getAllFromSerie($serie_id);
 		foreach ($matches as &$match){
 			$match['player1_id'] = $match['player1'];
 			$match['player2_id'] = $match['player2'];
 			$match['player3_id'] = $match['player3'];
 			$match['player4_id'] = $match['player4'];
 			$matchCreated = new Match($match);
 			$arrMatches[] = $matchCreated;
 		}

		return $arrMatches;
	}
	
	private function _setPlayers($match){
		$match->Player1(new Player(array("id" => $match->getPlayer1())));
		return $match;
	}

	private function _getMatch($arrMatches){
		$Matches = array();
		foreach ($arrMatches as $match){
			$currentMatch = new Match($match);
			$Matches[] = $currentMatch;
		}
		return $Matches;
	}
}

?>